import plugin from "../../../lib/plugins/plugin.js";
import Show from "../model/show.js";
import puppeteer from "../../../lib/puppeteer/puppeteer.js";
import data from '../model/XiuxianData.js'
import { get_random_talent, player_efficiency } from '../apps/xiuxian.js'
import { __PATH } from "./xiuxian.js"

/**
 * 生图模块
 */
/**
 * 指令失效
 */

export class showData extends plugin {
    constructor(e) {
        super({
            name: "showData",
            dsc: "修仙存档展示",
            event: "message",
            priority: 600,
            rule: [
                {
                    reg: "^#(我的装备)$",
                    fnc: "show_equipment",
                },
                {
                    reg: "^#境界列表$",
                    fnc: "show_Level",
                },
                {
                    reg: "^#柠檬堂$",
                    fnc: "show_ningmenghome",
                },
                {
                    reg: "^#万宝楼$",
                    fnc: "show_valuables",
                },
                {
                    reg: "^#武器楼$",
                    fnc: "show_valuables_arms",
                },
                {
                    reg: "^#丹药楼$",
                    fnc: "show_valuables_drug",
                },
                {
                    reg: "^#功法楼$",
                    fnc: "show_valuables_skill",
                },
                {
                    reg: "^#我的宗门$",
                    fnc: "show_association",
                },
                {
                    reg: "^#(修仙版本|更新预告)$",
                    fnc: "show_updata",
                }

            ]
        });

        this.path = __PATH.player_path;
    }


    async show_equipment(e) {
        let img = await get_equipment_img(e);
        e.reply(img);
        return;
    }


    async show_Level(e) {
        let img = await get_state_img(e);
        e.reply(img);
        return;
    }

    //柠檬堂
    async show_ningmenghome(e) {
        let img = await get_ningmenghome_img(e);
        e.reply(img);
        return;
    }

    //万宝楼
    async show_valuables(e) {
        let img = await get_valuables_img(e);
        e.reply(img);
        return;
    }

    //武器楼
    async show_valuables_arms(e) {
        let img = await get_valuables_arms_img(e);
        e.reply(img);
        return;
    }


    //丹药楼
    async show_valuables_drug(e) {
        let img = await get_valuables_drug_img(e);
        e.reply(img);
        return;
    }

    //功法楼
    async show_valuables_skill(e) {
        let img = await get_valuables_skill_img(e);
        e.reply(img);
        return;
    }

    //我的宗门
    async show_association(e) {
        let img = await get_association_img(e);
        e.reply(img);
        return;
    }

    //更新记录
    async show_updata(e) {
        let img = await get_updata_img(e);
        e.reply(img);
        return;
    }

}

//////////////////////////////////////////////////

/**
 * 返回该玩家的存档图片
 * @return image
 */
export async function get_player_img(e) {
    let usr_qq = e.user_id;
    let player = await data.getData("player", usr_qq);
    let equipment = await data.getData("equipment", usr_qq);
    let player_status = await getPlayerAction(usr_qq);
    let status = "空闲";
    if (player_status.time != null) {
        status = player_status.action + "(剩余时间:" + player_status.time + ")";
    }
    let lingshi = Math.trunc(player.灵石);
    if (player.灵石 > 9999999999) {
        lingshi = 9999999999;//太多不美观
    }
    if (player.宣言 == null || player.宣言 == undefined) {
        player.宣言 = "这个人很懒什么都没写"
    }
    if (player.灵根 == null || player.灵根 == undefined) {
        player.灵根 = await get_random_talent();
    }
    await data.setData("player", usr_qq, player);
    await player_efficiency(usr_qq);

    //这几行千万别写进存档里面去!!!!!!
    let this_association = {};
    if (!isNotNull(player.宗门)) {
        this_association = {
            "宗门名称": "无",
            "职位": "无"
        };
    } else {
        this_association = player.宗门;
    }

    let player_data = {
        user_id: usr_qq,
        nickname: player.名号,
        linggen: player.灵根,
        declaration: player.宣言,
        exp: player.修为,
        level: player.境界,
        lingshi: lingshi,
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量,
        arms: equipment.武器,
        armor: equipment.护具,
        treasure: equipment.法宝,
        talent: parseInt(player.修炼效率提升 * 100),
        player_action: status,
        learned_gongfa: player.学习的功法,
        association: this_association
    }

    const data1 = await new Show(e).get_playerData(player_data);
    let img = await puppeteer.screenshot("player", {
        ...data1,
    });

    return img;
}


/**
 * 我的宗门
 * @return image
 */
export async function get_association_img(e) {
    let usr_qq = e.user_id;
    //无存档
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    //门派
    let player = data.getData("player", usr_qq);
    if (!isNotNull(player.宗门)) {
        e.reply("尚未加入宗门");
        return;
    }


    //有加入宗门
    let ass = data.getAssociation(player.宗门.宗门名称);
    //寻找
    let mainqq = await data.getData("player", ass.宗主);
    //门槛
    let level = data.Level_list.find(item => item.level_id === ass.最低加入境界).level;


    //长老
    var zhanglao = [];
    for (var item in ass.长老) {
        zhanglao[item] = data.getData("player", ass.长老[item]).名号
    }
    //内门弟子
    var neimen = [];
    for (var item in ass.内门弟子) {
        neimen[item] = data.getData("player", ass.内门弟子[item]).名号
    }
    //外门弟子
    var waimen = [];
    for (var item in ass.外门弟子) {
        waimen[item] = data.getData("player", ass.外门弟子[item]).名号
    }

    var state = "需要维护";

    let now = new Date();
    let nowTime = now.getTime(); //获取当前日期的时间戳
    if (ass.维护时间 > nowTime - 1000 * 60 * 60 * 24 * 7) {
        state = "不需要维护";
    }

    //计算修炼效率
    var xiulian = ass.宗门等级 * 0.05 * 100;
    xiulian = Math.trunc(xiulian);

    let association_data = {
        user_id: usr_qq,
        ass: ass,
        mainqq: mainqq.名号,
        xiulian: xiulian,
        level: level,
        zhanglao: zhanglao,
        neimen: neimen,
        waimen: waimen,
        state: state
    }

    const data1 = await new Show(e).get_associationData(association_data);
    let img = await puppeteer.screenshot("association", {
        ...data1,
    });
    return img;

}


/**
 * 返回该玩家的装备图片
 * @return image
 */
export async function get_equipment_img(e) {
    let usr_qq = e.user_id;
    let player = await data.getData("player", usr_qq);
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    var bao = Math.trunc(parseInt(player.暴击率 * 100))
    let equipment = await data.getData("equipment", usr_qq);
    let player_data = {
        user_id: usr_qq,
        nickname: player.名号,
        arms: equipment.武器,
        armor: equipment.护具,
        treasure: equipment.法宝,
        player_atk: player.攻击,
        player_def: player.防御,
        player_bao: bao,
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量

    }
    const data1 = await new Show(e).get_equipmnetData(player_data);
    let img = await puppeteer.screenshot("equipment", {
        ...data1,
    });

    return img;
}

/**
 * 返回该玩家的纳戒图片
 * @return image
 */
export async function get_najie_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let player = await data.getData("player", usr_qq);
    let najie = await data.getData("najie", usr_qq);
    var lingshi = Math.trunc(najie.灵石);
    var lingshi2 = Math.trunc(najie.灵石上限);
    let player_data = {
        user_id: usr_qq,
        nickname: player.名号,
        najie_lv: najie.等级,
        player_maxHP: player.血量上限,
        player_nowHP: player.当前血量,
        najie_maxlingshi: lingshi2,
        najie_lingshi: lingshi,
        najie_equipment: najie.装备,
        najie_danyao: najie.丹药,
        najie_daoju: najie.道具,
        najie_gongfa: najie.功法,
        najie_caoyao: najie.草药

    }
    const data1 = await new Show(e).get_najieData(player_data);
    let img = await puppeteer.screenshot("najie", {
        ...data1,
    });

    return img;

}


/**
 * 返回境界列表图片
 * @return image
 */

export async function get_state_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let Level_list = data.Level_list;
    let state_data = {
        user_id: usr_qq,
        Level_list: Level_list
    }
    const data1 = await new Show(e).get_stateData(state_data);
    let img = await puppeteer.screenshot("state", {
        ...data1,
    });
    return img;

}

/**
 * 返回柠檬堂
 * @return image
 */

export async function get_ningmenghome_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let commodities_list = data.commodities_list;
    let ningmenghome_data = {
        user_id: usr_qq,
        commodities_list: commodities_list
    }
    const data1 = await new Show(e).get_ningmenghomeData(ningmenghome_data);
    let img = await puppeteer.screenshot("ningmenghome", {
        ...data1,
    });
    return img;

}


/**
 * 返回万宝楼
 * @return image
 */

export async function get_valuables_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let daoju_list = data.daoju_list;
    let valuables_data = {
        user_id: usr_qq,
        daoju_list: daoju_list
    }
    const data1 = await new Show(e).get_valuablesData(valuables_data);
    let img = await puppeteer.screenshot("valuables", {
        ...data1,
    });
    return img;

}

/**
 * 返回武器楼
 * @return image
 */

export async function get_valuables_arms_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let equipment_list = data.equipment_list;
    let valuables_data = {
        user_id: usr_qq,
        equipment_list: equipment_list,
    }
    const data1 = await new Show(e).get_valuables_armsData(valuables_data);
    let img = await puppeteer.screenshot("valuables_arms", {
        ...data1,
    });
    return img;

}



/**
 * 返回丹药楼
 * @return image
 */
export async function get_valuables_drug_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let danyao_list = data.danyao_list;
    let valuables_data = {
        user_id: usr_qq,
        danyao_list: danyao_list
    }
    const data1 = await new Show(e).get_valuables_drugData(valuables_data);
    let img = await puppeteer.screenshot("valuables_drug", {
        ...data1,
    });
    return img;

}


/**
 * 返回功法楼
 * @return image
 */

export async function get_valuables_skill_img(e) {
    let usr_qq = e.user_id;
    let ifexistplay = data.existData("player", usr_qq);
    if (!ifexistplay) {
        return;
    }
    let gongfa_list = data.gongfa_list;
    let valuables_data = {
        user_id: usr_qq,
        gongfa_list: gongfa_list
    }
    const data1 = await new Show(e).get_valuables_skillData(valuables_data);
    let img = await puppeteer.screenshot("valuables_skill", {
        ...data1,
    });
    return img;

}





/**
 * 返回修仙版本
 * @return image
 */

export async function get_updata_img(e) {

    let updata_data = {
    }
    const data1 = await new Show(e).get_updataData(updata_data);
    let img = await puppeteer.screenshot("updata", {
        ...data1,
    });
    return img;

}


export async function get_ranking_power_img(e, Data, usr_paiming, thisplayer) {
    let usr_qq = e.user_id;
    //let thisplayer = await data.getData("player", usr_qq);

    let ranking_power_data = {
        user_id: usr_qq,
        nickname: thisplayer.名号,
        exp: thisplayer.修为,
        level: thisplayer.境界,
        usr_paiming: usr_paiming,
        allplayer: Data
    }
    const data1 = await new Show(e).get_ranking_powerData(ranking_power_data);
    let img = await puppeteer.screenshot("ranking_power", {
        ...data1,
    });
    return img;
}


export async function get_ranking_money_img(e, Data, usr_paiming, thisplayer, thisnajie) {
    let usr_qq = e.user_id;
    //let thisplayer = await data.getData("player", usr_qq);
    var najie_lingshi = Math.trunc(thisnajie.灵石);
    var lingshi = Math.trunc(thisplayer.灵石 + thisnajie.灵石);
    let ranking_money_data = {
        user_id: usr_qq,
        nickname: thisplayer.名号,
        lingshi: lingshi,
        najie_lingshi: najie_lingshi,
        usr_paiming: usr_paiming,
        allplayer: Data
    }
    const data1 = await new Show(e).get_ranking_moneyData(ranking_money_data);
    let img = await puppeteer.screenshot("ranking_money", {
        ...data1,
    });
    return img;
}



async function getPlayerAction(usr_qq) {
    //查询redis中的人物动作
    let arr = {};
    let action = await redis.get("xiuxian:player:" + usr_qq + ":action");
    action = JSON.parse(action);
    if (action != null) {
        //人物有动作查询动作结束时间
        let action_end_time = action.end_time;
        let now_time = new Date().getTime();
        if (now_time <= action_end_time) {
            let m = parseInt((action_end_time - now_time) / 1000 / 60);
            let s = parseInt(((action_end_time - now_time) - m * 60 * 1000) / 1000);
            arr.action = action.action;//当期那动作
            arr.time = m + "分" + s + "秒";//剩余时间
            return arr;
        }
    }
    arr.action = "空闲";
    return arr;
}


/**
 * 判断对象是否不为undefined且不为null
 * @param obj 对象
 * @returns obj==null/undefined,return false,other return true
 */
function isNotNull(obj) {
    if (obj == undefined || obj == null)
        return false;
    return true;
}